﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.SupplierCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.SupplierCommandHandler
{
    public class DeleteSupplierCommandHandler : IRequestHandler<DeleteSupplierCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_Supplier> suppRep;

        public DeleteSupplierCommandHandler(IBaseRepository<SU_Supplier> suppRep)
        {
            this.suppRep = suppRep;
        }
        public Task<APIResult<int>> Handle(DeleteSupplierCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list = suppRep.GetAll().Result.Where(x=>x.SupId==request.SupId).FirstOrDefault();
            var res= suppRep.Delete(list).Result;
            if (res > 0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }
    }
}
